package com.leyou.cart.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.cart.config.JwtProperties;
import com.leyou.cart.entity.Cart;
import com.leyou.cart.interceptors.LoginInterceptor;
import com.leyou.common.auth.entity.Payload;
import com.leyou.common.auth.entity.UserInfo;
import com.leyou.common.auth.utils.JwtUtils;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.threadLocals.UserHolder;
import com.leyou.common.utils.CookieUtils;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@EnableConfigurationProperties(JwtProperties.class)
public class CartService {


    @Autowired
    private JwtProperties prop;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private String KEY_PREFIX="ly:cart:user:id";

    public void addCart(Cart cart) {

        UserInfo userInfo = LoginInterceptor.getUserInfo();
        Long id = userInfo.getId();
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX+id.toString());
        if (ops.hasKey(cart.getSkuId().toString())) {
            String cartJson = ops.get(cart.getSkuId().toString());
            Cart storeCart = JsonUtils.nativeRead(cartJson, new TypeReference<Cart>() {
            });
            storeCart.setNum(cart.getNum()+storeCart.getNum());
            ops.put(storeCart.getSkuId().toString(),JsonUtils.toString(storeCart));

        } else {
            ops.put(cart.getSkuId().toString(), JsonUtils.toString(cart));
        }
    }

    public List<Cart> listCart() {

        UserInfo userInfo = LoginInterceptor.getUserInfo();
        Long id = userInfo.getId();
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX + id.toString());
        List<String> values = ops.values();
        List<Cart> carts=new ArrayList<>();
        if (null!=values && 0!=values.size()){

            for (String value:values
                    ) {
                Cart cart = JsonUtils.nativeRead(value, new TypeReference<Cart>() {
                });
                carts.add(cart);
            }

            return carts;
        }
        throw new LyException(ExceptionEnum.CART_NOT_FOUND);
    }

    public void updateCart(Long id,Integer num) {
        UserInfo userInfo = UserHolder.getUser();
        Long lid = userInfo.getId();
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX + lid.toString());
        String cartJson = ops.get(id.toString());
        if (!ops.hasKey(id.toString())){
            return;
        }

        Cart cart = JsonUtils.nativeRead(cartJson, new TypeReference<Cart>() {
        });
        if (num>=1){
        cart.setNum(num);}
        ops.put(id.toString(),JsonUtils.toString(cart));

    }

    public void deleteCart(Long skuId) {
        UserInfo userInfo = UserHolder.getUser();
        Long lid = userInfo.getId();
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps(KEY_PREFIX + lid.toString());
        if (!ops.hasKey(skuId.toString())){
            return;
        }
        ops.delete(skuId.toString());

    }

    public void mergeCarts(List<Cart> carts) {
        for (Cart cart:carts
             ) {
            addCart(cart);
        }

    }
}
